package com.leetcode.problems032;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/longest-valid-parentheses/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public int longestValidParentheses(String s) {
        if(s == null || s.length() <= 1){
            return 0;
        }
        Stack<Integer> stack = new Stack<>();
        int maxans = 0;
        stack.push(-1);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.push(i);
            } else {
                stack.pop();
                if (stack.isEmpty()) {
                    stack.push(i);
                } else {
                    maxans = Math.max(maxans, i - stack.peek());
                }
            }
        }
        return maxans;
    }

    public static void main(String[] args) {
        String words = "))";
        System.out.println(new Solution().longestValidParentheses(words));
    }
}
